package com.example.springeventlistenersample.listener;

import com.example.springeventlistenersample.SpringEvent;
import com.example.springeventlistenersample.UserInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

@Component
public class MyEventListener {
    Logger log = LoggerFactory.getLogger(MyEventListener.class);
    /**
     * 注册监听实现方法
     *当有我们主动调用 UserService.publish方法时，事件会被回调到这里
     */
    @EventListener
    public void receive(SpringEvent<UserInfo> event) {
        // 获取注册用户对象
        UserInfo user = event.getData();
        // 打印注册用户信息
        log.info("@EventListener注册信息，用户名："+user.getName()+"，年龄："+user.getAge());
    }
}
